浅析被遗忘的SQLServer比较运算符修饰词
select * from t2 where n= any (select n from t1 ) --2,3
官方的参考文档
select * from t2 a where not exists(select 1 from t1 where n>=a.n)
select * from t2 where n >all(select n from t1)
insert into t1 select 2 union select 3
他们作用于比较运算符和子查询之间,作用类似Exists、not exists、in、not in以及其他逻辑意义,这些语法同样被SQLServer2000支持但是很少看到有人用它们。
select * from t2 where n< any (select n from t1 ) --1,2
select * from t2 where n= all (select n from t1 ) --无数据
--select * from t2 where n<some(select n from t1) --1,2
set nocount on
select * from t2 where n<> any (select n from t1 ) --1,2,3,4
if (object_id ('t2' ) is not null ) drop table t2
select * from t2 where n> any (select n from t1 ) --3,4
select * from t2 where n> all (select n from t1 ) --4
create table t1 (n int )
--select * from t2 where n=some(select n from t1) --2,3
set nocount off
--select * from t2 where n>some(select n from t1) --3,4
他们逻辑上意义很像但是对于null的处理却是恰恰相反,第一句会忽略子查询的null而把t2的null同时查出来,第二句却是忽略了t2的null同时会因为t1中的null而无法查询到数据。
注意,如果t1中包含null数据,那么所有All相关的比较运算将不会返回任何结果,原因就不用多解释了。而因为t1和t2表的null的存在他们和not exists之类的比较符会有一些区别。
if (object_id ('t1' ) is not null ) drop table t1
比如下面两句
create table t2 (n int )
use tempdb
SQLServer中有三个关键字可以修改比较运算符:All、Any和Some,其中Some和Any等价。
%28SQL.90%29.aspx
select * from t2 where n< all (select n from t1 ) --1
select * from t2 where n<> all (select n from t1 ) --1,4
--select * from t2 where n<>some(select n from t1)--1,2,3,4
复制代码 代码如下:
go相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/sql/mssql/1830.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
SQL基本教程之行转列Pivo
时间:2021-01-20
-
region from hr.Employees union
时间:2021-01-20
-
有时候需要调整用户权限
时间:2021-01-19
-
(但使用 ORDER BY 子句并不
时间:2021-01-19
-
RAND()*10000)insert into Detail
时间:2021-01-19
-
OR 运算符:在两侧的查询
时间:2021-01-19
-
放假之前老大跟我提起了
时间:2021-01-19
-
数据库的运维计策剧本篇
时间:2021-01-19
热门文章
-
4.与聚合函数和 GROUP BY 子句有关的常见错
时间:2021-01-19
-
SQL Server安全(11/11):审核(Auditing)
时间:2021-01-09
-
sqlserver中查询横表变竖表的sql语句简析
时间:2020-12-08
-
SQL Server简单模式下误删除堆表记录恢复方
时间:2020-12-12
-
关于SQL Server查询语句的使用
时间:2020-12-13
-
MSSQL教程_mssql数据库教程_MSSQL基础教程_第
时间:2020-12-13
-
jdbc连接sql server数据库问题分析
时间:2020-12-10
-
详解SQL游标的用法
时间:2020-12-27
-
sql server 关于设置null的一些建议
时间:2020-12-28
-
mssql关于一个表格结构的另外一种显示(表
时间:2020-12-11
